package com.chinafree.business.service.impl;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.chinafree.common.utils.JWTUtils;
import com.chinafree.model.enumeration.JWTValue;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

public class BaseServiceImpl {
    @Autowired
    private HttpServletRequest request;

    protected Long getUserId() {
        String token = this.request.getHeader("token");
        DecodedJWT tokenInfo = JWTUtils.getTokenInfo(token);
        String userId = tokenInfo.getHeaderClaim(JWTValue.USER_ID.getValue()).asString();
        return Long.valueOf(userId);
    }

    protected Long getRoleId() {
        String token = this.request.getHeader("token");
        DecodedJWT tokenInfo = JWTUtils.getTokenInfo(token);
        String roleId = tokenInfo.getHeaderClaim(JWTValue.ROLE_ID.getValue()).asString();
        return Long.valueOf(roleId);
    }

    protected Long getCompanyId() {
        String token = this.request.getHeader("token");
        DecodedJWT tokenInfo = JWTUtils.getTokenInfo(token);
        String companyId = tokenInfo.getHeaderClaim(JWTValue.COMPANY_ID.getValue()).asString();
        return Long.valueOf(companyId);
    }

    protected Long getCompanyId(String token) {
        DecodedJWT tokenInfo = JWTUtils.getTokenInfo(token);
        String companyId = tokenInfo.getHeaderClaim(JWTValue.COMPANY_ID.getValue()).asString();
        return Long.valueOf(companyId);
    }
}
